package provisioner.util;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;

public class OSShell {

   public static int exec(String command) throws Exception {
      int exitVal = 1;
      try {
         // run the Unix command
         Process p = Runtime.getRuntime().exec(command);

         BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));

         BufferedReader stdError = new BufferedReader(new InputStreamReader(p.getErrorStream()));

         String line = null;
         // read the output from the command
         while ((line = stdInput.readLine()) != null) {
            System.out.println(line);
         }
         stdInput.close();

         // read any errors from the attempted command
         // don't move upward since the clp will be blocked when output is full
         boolean haveError = false;
         while ( (line = stdError.readLine()) != null) {
            System.err.println(line);
            haveError = true;
         }
         if (haveError) {
             System.exit(1);
         }

         stdError.close();
         exitVal = p.waitFor();
         //System.out.println("Exited with error code "+exitVal);
      }
      catch (IOException e) {
         e.printStackTrace();
      }

      return exitVal;

   }

   public static int exec(String[] commands) throws Exception {
      int exitVal = 1;
      try {
         // run the Unix command
         Process p = Runtime.getRuntime().exec(commands);

         BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));

         BufferedReader stdError = new BufferedReader(new InputStreamReader(p.getErrorStream()));

         String line = null;
         // read the output from the command
         while ((line = stdInput.readLine()) != null) {
            System.out.println(line);
         }
         stdInput.close();

         // read any errors from the attempted command
         // don't move upward since the clp will be blocked when output is full
         boolean haveError = false;
         while ( (line = stdError.readLine()) != null) {
            System.err.println(line);
            haveError = true;
         }
         if (haveError) {
             System.exit(1);
         }

         stdError.close();
         exitVal = p.waitFor();
         //System.out.println("Exited with error code "+exitVal);
      }
      catch (IOException e) {
         e.printStackTrace();
      }

      return exitVal;

   }

   public static String execAndGetStdout(String command) throws Exception {
      StringBuffer sb = new StringBuffer();
      try {
         // run the Unix command
         Process p = Runtime.getRuntime().exec(command);

         BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));

         BufferedReader stdError = new BufferedReader(new InputStreamReader(p.getErrorStream()));

         String line = null;
         // read the output from the command
         while ((line = stdInput.readLine()) != null) {
            sb.append(line + "\n");
         }
         stdInput.close();

      }
      catch (IOException e) {
         e.printStackTrace();
      }

      return sb.toString();

   }

   public static void main(String[] args) throws Exception {
      OSShell.exec("ls");
      String[] cs = {"ls","-l"};
      OSShell.exec(cs);
      System.out.println(OSShell.execAndGetStdout("ps -ax"));
   }

}
